package nio.demo;

import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Desc:   内存映射文件 I/O
 * @author: cww
 * @DateTime: 2020-02-05 22:16
 */

public class MappedByteBufferExample {
    /**
     * 将一个 FileChannel(它的全部或者部分)映射到内存中
     * @param args
     */
    public static void main(String[] args) throws Exception{
        // 将文件的钱 1024 个字节映射到内存中
        RandomAccessFile file = new RandomAccessFile("src\\maindemo\\resources\\file\\nio\\nio-data.txt", "rw");
        FileChannel fc = file.getChannel();
        MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, 0, 1024);
        while (mbb.hasRemaining()) {
            System.out.println(mbb.get());
        }
    }
}
